//
//  ViewController.m
//  Rac
//
//  Created by 崔博 on 2017/12/28.
//  Copyright © 2017年 崔博. All rights reserved.
//  组合。保证信号 响应的顺讯

#import "ViewController.h"
#import <ReactiveObjC/ReactiveObjC.h>

@interface ViewController ()
{
    RACSignal *_sigA;
    RACSignal *_sigB;
    RACSignal *_sigC;
}
@property (weak, nonatomic) IBOutlet UIButton *btn;
@property (weak, nonatomic) IBOutlet UITextField *accountF;
@property (weak, nonatomic) IBOutlet UITextField *pwdF;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    // 1、创建信号
    RACSubject *signal = [RACSubject subject];
    
    // 2、处理信号，订阅信号
    [[signal take:1] subscribeNext:^(id x) {
        
        NSLog(@"%@",x);
    }];
    
    // 3.发送信号
    [signal sendNext:@1];
    
    [signal sendNext:@2];
    
//    RACSignal * nameSig = [self.accountF rac_textSignal];
//    RACSignal * pwd = [self.pwdF rac_textSignal];
//
//
//    RACSignal * reduceSignal = [RACSignal combineLatest:@[nameSig,pwd] reduce:^id(NSString * name,NSString * age){
//
//        return [NSString stringWithFormat:@"姓名:%@,年龄:%@",name,age];
//
//    }];
//
//    [reduceSignal subscribeNext:^(id x) {
//
//        NSLog(@"%@",x);
//    }];


//    RACSignal * sig =  [[tupleSig map:^id _Nullable(id  _Nullable value) {
//
//        return [[RACSignal return:value] delay:1];
//    }] concat];
//
//    [sig subscribeNext:^(id  _Nullable x) {
//        NSLog(@"%@",x);
//    }];
//
//    // 改变某个值的个数
//    RACSignal *mappSingal = [[tupleSig map:^id _Nullable(NSNumber * value) {
//        return [[[RACSignal return:value] repeat] take:value.integerValue];
//    }] flatten];
//
//    [mappSingal subscribeNext:^(id  _Nullable x) {
//        NSLog(@"%@",x);
//    }];
    
//    // 将一个值改为一个错误
//    RACSignal *mapSinga1 = [tupleSig map:^id _Nullable(NSNumber * value) {
//        if (value.integerValue == 0) {
//            return [RACSignal error:[NSError errorWithDomain:@"0" code:0 userInfo:nil]];
//        }else
//        {
//            return [RACSignal return:value];
//        }
//    }];
//    [mapSinga1 subscribeNext:^(id  _Nullable x) {
//        NSLog(@"%@",x);
//    } error:^(NSError * _Nullable error) {
//        NSLog(@"%d",error.code);
//    }];

    // 改变时间间隔
//    RACChannelTerminal *termina = self.accountF.rac_newTextChannel;
//    [[termina map:^id _Nullable(id  _Nullable value) {
//        NSLog(@"");
//        return @"123";
//    }] subscribe:termina];
//    [[self.accountF.rac_textSignal flatten:^id _Nullable(NSString * _Nullable value) {
//        NSLog(@"-----%@",value);
//        return @"1";
//    }] subscribeNext:^(id  _Nullable x) {
//        NSLog(@"%@",x);
//    }];
//    [[self.accountF.rac_textSignal flattenMap:^__kindof RACSignal * _Nullable(NSString * _Nullable value) {
//        NSLog(@"------%@",value);
//        return <#expression#>
//    }] subscribeNext:^(id  _Nullable x) {
//        NSLog(@"%@",x);
//    }];;
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 创建信号中的信号
    RACSubject *signalOfsignals = [RACSubject subject];
    RACSubject *signal = [RACSubject subject];
    
    [[signalOfsignals flattenMap:^__kindof RACSignal * _Nullable(id  _Nullable value) {
        NSLog(@"----%@",value);
        return value;
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    
    
    // 信号的信号发送信号
    
    
    // 信号发送内容
    RACSignal * s = [signal zipWith:[RACSignal return:@"1"]];
    [signalOfsignals sendNext:s];
    return ;
    _sigA = [[[RACSignal return:@"1"] repeat] delay:1];
    _sigB = [RACSignal return:@"2"];
    _sigC = [_sigA sample:_sigB];
    [_sigC subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    _sigA = [RACSignal return:@(NO)];
    
    RACSignal * thenSig = [RACSignal return:@(YES)];
    RACSignal * eleSig = [RACSignal return:@"123"];
    
    
    RACSignal * sig = [[_sigA map:^id _Nullable(NSNumber * value) {
        return value.boolValue ? thenSig : eleSig;
    }] switchToLatest];
    [sig subscribeNext:^(id  _Nullable x) {
        NSLog(@"");
    }];
    
    // FlattenMap
//    RACSignal *signal = [RACSignal return:@"http//xxx"];
//    RACSignal *getSignal = [signal flattenMap:^__kindof RACSignal * _Nullable(NSString * value) {
//        NSURLRequest * requ = [NSURLRequest requestWithURL:[NSURL URLWithString:value]];
//        return [NSURLConnection rac_sendAsynchronousRequest:requ];
//    }];
//    RACSignal *jsonSignal = [getSignal flattenMap:^__kindof RACSignal * _Nullable(NSData * value) {
//        NSError * error = nil;
//        id result = [NSJSONSerialization JSONObjectWithData:value options:0 error:&error];
//        return error == nil ? [RACSignal return:result] : [RACSignal error:error];
//    }];
//
//    RACSignal * getItemSinga = [jsonSignal flattenMap:^__kindof RACSignal * _Nullable(NSDictionary * value) {
//        if (![value isKindOfClass:[NSDictionary class]] || value[@"data.url"] == nil) {
//            return [RACSignal error:nil];
//        }
//        NSURLRequest * anthoerReq = [NSURLRequest requestWithURL:[NSURL URLWithString:value[@"data.url"]]];
//        return [NSURLConnection rac_sendAsynchronousRequest:anthoerReq];
//    }];
}
- (void)zip{
    // zipwith 两个新号压缩！只要当两个新号同时发出信号内容，并且将内容合成为一个元祖给你
    
    // 创建信号
    RACSubject * suA = [RACSubject subject];
    RACSubject * suB = [RACSubject subject];
    // 压缩
    RACSignal * zipsig = [suA zipWith:suB];
    
    // 接受数据 和发送数据无关。只和压缩 顺讯有关系
    [zipsig subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    // 发送数据  A和B同时发送信号 才会被订阅
    
    //                                                                                 1,
    //                                                                                 2
    //                                                                                 )
    //                                                                                 11,
    //                                                                                 22
    //                                                                                 )111,222 )
    [suA sendNext:@"1"];
    [suB sendNext:@"2"];
    
    [suA sendNext:@"11"];
    [suB sendNext:@"22"];
    
    [suA sendNext:@"111"];
    [suB sendNext:@"222"];
    
    UITableView * table = [[UITableView  alloc] initWithFrame:self.view.bounds style:0];
    [self.view addSubview:table];
    
}
- (void)merge{
    // 创建信号
    RACSubject * subA = [RACSubject subject];
    RACSubject * subB = [RACSubject subject];
    RACSubject * subC = [RACSubject subject];
    // 发送数据
    [subA sendNext:@"A"];
    [subB sendNext:@"B"];
    
    // 组个新号
    RACSubject * meSub = [RACSubject merge:@[subA,subA,subC]];
    
    [meSub subscribeNext:^(id  _Nullable x) {
        
    }];
}
- (void)then{
    RACSignal * singal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"数据1"];
        [subscriber sendCompleted];
        NSLog(@"发送请求A");
        return nil;
    }];
    
    
    RACSignal * singal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"数据2"];
        NSLog(@"发送请求B");
        [subscriber sendCompleted];
        return nil;
    }];
    
    RACSignal * thenSig = [singal1 then:^RACSignal * _Nonnull{
        return singal;
    }];
    
    [thenSig subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
}
- (void)concat{
    // 组合
    // 创建信号
    RACSignal * singal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"数据"];
        [subscriber sendCompleted];
        NSLog(@"发送请求A");
        return nil;
    }];
    
    
    RACSignal * singal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"数据"];
        NSLog(@"发送请求B");
        [subscriber sendCompleted];
        return nil;
    }];
    RACSignal * singal2 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"数据"];
        NSLog(@"发送请求B");
        return nil;
    }];
    
    // concat :按顺序组个
    // 创建组个函数
    //    RACSignal * canratSinal = [[singal concat:singal1] concat:singal2];
    RACSignal * canratSinal = [RACSignal concat:@[singal,singal1,singal2]];
    
    [canratSinal subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
}

@end
